Ext.define('Isms.controller.DynController', {
	extend : 'Ext.app.Controller',
	stores : ['DynStore'],
	models : [],
	views : ['dyn-ent.DynList', 'dyn-ent.PostDyn', 'dyn-ent.EditDyn','Ext.window.MessageBox',
			'Ext.tip.*'],
	init : function() {
		var vNewsAdd = null;
		this.control({
					'dyn-list>grid' : {
						render : function() {
							var grid = Ext.getCmp('dyn_grid');
							grid.store.loadPage(1);
						}
					},
					'dyn-list button[action = dyn_add_action]' : {
						click : this.showAddPanel
					},
					'dyn-list button[action = dyn_edit_action]' : {
						click : this.showEditPanel
					},
					'dyn-list button[action = dyn_del_action]' : {
						click : this.handelDel
					},
					'edit-dyn button[action = dyn_edit_action]' : {
						click : this.handelEdit
					},
					'post-dyn button[action = dyn_post_action]' : {
						click : this.handelAdd
					}
				});
	},
	handelDel : function(btn) {
							Ext.MessageBox.confirm('警告！！', '确定删除？？？？', function(btn) {
						if (btn == "yes") {
							var mFile_list = Ext.getCmp('dyn_grid');
							var mDatas = mFile_list.getSelectionModel()
									.getSelection();
							var params = {};
							params.id = new Array();
							for (var i = 0; i < mDatas.length; i++) {
								params.id[i] = mDatas[i].get('id');
							}
							Ext.Ajax.request({
										url : '../dyn/del.do',
										params : params,
										method : 'get',// 请求方式
										success : function(respsonse) {
											Ext.Msg.alert('提示', '删除成功',
													function() {
														// 重新加载数据
														Ext.getCmp('dyn_grid').store
																.loadPage(1);
													});

										},
										failure : function(respsonse) {
											Ext.Msg.alert('提示', '删除失败');
										}
									});

						}
					});

	},
	handelEdit : function(btn) {
		var form = btn.up('form').getForm();
		if (form.isValid()) {
			form.submit({
						url : '../dyn/edit.do',
						params : form.getValues(true),
						method : 'POST',
						waitTitle : '请稍后',
						waitMsg : '正在上传文档文件 ...',
						success : function(form, action) {
							Ext.MessageBox.alert("提示", "操作成功！", function() {
										// 重新加载数据
										Ext.getCmp('dyn_grid').store
												.loadPage(1);
										// 关闭窗口
										vDynEdit.close();
									});
						},
						failure : function(form, action) {
							Ext.MessageBox.alert("错误！",
									"操作失败<br>服务报告了一项错误，或连接失败");
						}
					});
		}
	},
	handelAdd : function(btn) {
		var form = btn.up('form').getForm();
		if (form.isValid()) {
			form.submit({
						url : '../dyn/add.do',
						params : form.getValues(true),
						method : 'POST',
						waitTitle : '请稍后',
						waitMsg : '正在上传文档文件 ...',
						success : function(form, action) {
							Ext.MessageBox.alert("提示", "操作成功！", function() {
										// 重新加载数据
										Ext.getCmp('dyn_grid').store
												.loadPage(1);
										// 关闭窗口
										vAddPanel.close();
									});
						},
						failure : function(form, action) {
							Ext.MessageBox.alert("错误！",
									"操作失败<br>服务报告了一项错误，或连接失败");
						}
					});
		}
	},
	showAddPanel : function(btn) {
		vAddPanel = Ext.create('Ext.window.Window', {
					title : 'Hello',
					height : 300,
					width : 600,
					items : [{
								xtype : 'post-dyn'
							}]
				});
		vAddPanel.show();
	},
	showEditPanel : function(btn) {
		var rec = Ext.getCmp('dyn_grid').getSelectionModel().getLastSelected();
		if (!vDynEdit) {
			vDynEdit = Ext.create('widget.window', {
						title : '修改 企业动态 信息',
						closable : true,
						closeAction : 'hide',
						width : 600,
						minWidth : 350,
						height : 350,
						items : [{
									xtype : 'edit-dyn'
								}]
					});
		}
		Ext.getCmp('d_id').setValue(rec.get('id'));
		Ext.getCmp('d_title').setValue(rec.get('title'));
		Ext.getCmp('d_content').setValue(rec.get('content'));

		vDynEdit.show();
	},
	news_add : function(obj) {
		if (obj.text == '添加') {
			var form = obj.up('form').getForm();
			if (form.isValid()) {
				form.submit({
							url : '../news/addNews.do',
							params : form.getValues(true),
							method : 'POST',
							// waitTitle : '请稍后',
							// waitMsg : '正在添加中……………',
							success : function() {
								Ext.MessageBox.alert("提示", "操作成功！", function() {
											vNewsAdd.close();
											var grid = Ext.getCmp('news_grid');
											grid.store.loadPage(1);
										});
							},
							failure : function(form, action) {
								Ext.MessageBox.alert("错误！",
										"操作失败<br>服务报告了一项错误，或连接失败");
							}
						});
			}
		};
	},
	newsItemClick : function(grid, record, item, index, e, eOpts) {
		// var itemdata=record.get
		var singleNews = Ext.create('widget.single-news');
		Ext.getCmp('vTitle').setValue(record.get('title'));
		Ext.getCmp('vContent').setValue(record.get('content'));
		var top = "否";
		if (record.get('cancle') == true)
			top = "是";
		Ext.getCmp('vTop').setValue(top);
		Ext.getCmp('vTitle').setValue(record.get('title'));
		var win = Ext.create('widget.window', {
					title : '查看文档',
					closable : true,
					closeAction : 'hide',
					width : 600,
					minWidth : 350,
					height : 350,
					items : [{
								xtype : singleNews
							}]
				});
		win.show();
	}
});
var vAddPanel = null;
var vDynEdit = null;
